var pageIndex = 0;     //页面索引初始值  
var pageSize = 15;     //每页显示条数初始化，修改显示条数，修改这里即可
$(function(){
	$("#Pagination").pagination(getCount(), {  
        callback: PageCallback,  
        prev_text: '上一页',       //上一页按钮里text  
        next_text: '下一页',       //下一页按钮里text  
        items_per_page: pageSize,  //显示条数  
        num_display_entries: 5,    //连续分页主体部分分页条目数  
        current_page: pageIndex,   //当前页索引  
        num_edge_entries: 5        //两侧首尾分页条目数  
    });    

    //翻页调用  
    function PageCallback(index, jq) { 
    	headTable();
        InitTable(index);  
    }  
    
    function getCount(){
    	var u = $.cookie("youleche.u");
    	if(u == null){
    		window.parent.location.href = "../noresponse.jsp";
    		return;
    	}
    	var count = "";
    	$.ajax({
        	type : 'get',
			dataType: 'text',
			contentType: "application/json",
			async: false, 
            url: "../sfixau.do?action=0&userName=" + u,      //提交到一般处理程序请求数据  
            success: function(data) { 
            	count = data;
            	if(count == 0){
            		$("#Pagination").hide();
            	}else{
            		$("#Pagination").show();
            		$("#spannum").text(data);
            	}
            }  
         });  
    	
    	return count;
    }
});

//请求数据  
function InitTable(pageIndex) { 
	var u = $.cookie("youleche.u");
	if( u != null){
	    $.ajax({
	    	type : 'get',
			dataType: 'json',
			contentType: "application/json",
			async: false, 
	        url: '../sfixau.do?action=1&userName=' + u + '&pageIndex=' + (pageIndex + 1) + '&pageSize=' + pageSize,      //提交到一般处理程序请求数据  
	        success: function(data) {
				$.each(data, function(index, value){
					$("#tableHeader").after(getInfoForTable(value.fixId,value.isGoHome,value.addTime,value.linkMan,value.phoneNum));
				});
	        }  
	    });
	}else{
		window.parent.location.href = "../noresponse.jsp";
	}
}

function getInfoForTable(fixId,isGoHome,addTime,linkMan,phoneNum){
	var sm = "是";
	if(isGoHome == "1"){
		sm = "否";
	}
	var str = "<tr>";
	str += "<td>"+sm+"</td>";
	str += "<td>"+linkMan+"</td>";
	str += "<td style=\"color: red;\">"+phoneNum+"</td>";
	str += "<td>"+dateFormat(addTime)+"</td>";
	str += "<td><a style=\"cursor:pointer;\" onclick=\"deletes('"+fixId+"')\">删除</a>&nbsp;&nbsp;<a style=\"cursor:pointer;\" onclick=\"modifys('"+fixId+"')\">修改</a>&nbsp;<a style=\"cursor:pointer;\" onclick=\"details('"+fixId+"')\">详细</a>&nbsp;<a style=\"cursor:pointer;\" onclick=\"tops('"+fixId+"')\">置顶</a></td>";
	str += "</tr>";
		
	return str;
}

function details(fixId){
	window.parent.location.href="../sfix.do?id="+fixId+"&action=2";
}

function tops(fixId){
	 $.ajax({
	    	type : 'get',
			dataType: 'text',
	        url: '../sfixau.do?action=2&fixId=' + fixId ,      //提交到一般处理程序请求数据  
	        success: function(data) {
				if(data == "1"){
					layer.alert("置顶成功~~~", 9);
				}else{
					layer.alert("置顶失败~~", 8);
				}
	        }  
	    });
}
function modifys(vehicleId){
	alert(vehicleId);
}

function deletes(vehicleId){
	alert(vehicleId);
}

function dateFormat(date){
	var formatDate = "";
	var month = date.split(' ')[0];
	var day = date.split(' ')[1];
	var year = date.split(' ')[2].replace(",","");
	switch(month){
		case "一月":
			month = "01";
			break;
		case "二月":
			month = "02";
			break;
		case "三月":
			month = "03";
			break;
		case "四月":
			month = "04";
			break;
		case "五月":
			month = "05";
			break;
		case "六月":
			month = "06";
			break;
		case "七月":
			month = "07";
			break;
		case "八月":
			month = "08";
			break;
		case "九月":
			month = "09";
			break;
		case "十月":
			month = "10";
			break;
		case "十一月":
			month = "11";
			break;
		case "十二月":
			month = "12";
			break;
	}
	
	day=day.replace(/,/ig,""); 
	if(day.length == 1){
		day = "0" + day;
	}
	
	formatDate = year + "-" + month + "-" + day;
	return formatDate;
}

function headTable(){
	var string = "";
	string += "<tr id=\"tableHeader\">";
	string += "<td>是否上门</td>";
	string += "<td>联系人</td>";
	string += "<td>联系电话</td>";
	string += "<td>发布时间</td>";
	string += "<td>操作</td>";
	string += "</tr>";
	
	$("#tableData").html(string);
}

function release(){
	window.parent.location.href = "../fix.jsp";
}